package com.buildings.util;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.buildings.base.BaseActivity;
import com.buildings.bean.VersionBean;
import com.buildings.fragment.UpdataFragmentDialog;

import java.io.File;
import java.util.HashMap;

/**
 * 检查更新工具类
 */
public class UpdateUtils {
    private static ProgressDialog progressDialog;
//    广播接受者
//    private static MyReceiver receiver;

    private static BaseActivity context;

    /**
     * 显示更新对话框,包含版本相关信息
     */
    public static void showUpdateConfirmDialog(final BaseActivity context, final VersionBean.DataDTO updateInfo, final boolean isForceCheck) {
        UpdateUtils.context = context;
        if (updateInfo.getIsForceUpdate().equals("1")) {
            //强制更新
            UpdataFragmentDialog.newInstance(updateInfo).show(context.getSupportFragmentManager(), "");
            return;
        }
        UpdataFragmentDialog.newInstance(updateInfo).show(context.getSupportFragmentManager(), "");
    }

    /**
     * 是否需要更新
     *
     * @param context
     * @param version
     * @return
     */
    public static Boolean isUpdata(Context context, int version) {
        if (version > HYAppUtils.getAppVersionCode(context)){
            return true;
        }
        return false;
    }

    /**
     * 版本号比较
     *
     * @param version1
     * @param version2
     * @return 0 版本号相等; 1 version1大于version2; -1 version1小于verison2
     */
    public static int compareVersion(String version1, String version2) {
        if (version1.equals(version2)) {
            return 0;
        }
        String[] version1Array = version1.split("\\.");
        String[] version2Array = version2.split("\\.");
        int index = 0;
        // 获取最小长度值
        int minLen = Math.min(version1Array.length, version2Array.length);
        int diff = 0;
        // 循环判断每位的大小
        while (index < minLen
                && (diff = Integer.parseInt(version1Array[index])
                - Integer.parseInt(version2Array[index])) == 0) {
            index++;
        }
        if (diff == 0) {
            // 如果位数不一致，比较多余位数
            for (int i = index; i < version1Array.length; i++) {
                if (Integer.parseInt(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Integer.parseInt(version2Array[i]) > 0) {
                    return -1;
                }
            }
            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }
    private static int lastPosition;

    /**
     * 下载进度
     */
    public static void showDowloadProgress() {
        progressDialog = new ProgressDialog(context);
        progressDialog.setTitle("正在更新...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条对话框//样式（水平，旋转）
        //进度最大值
        progressDialog.setMax(100);
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    /**
     * 安装apk
     *
     * @param context
     * @param
     */
    public static void installApk(Context context, String apkPath) {
        try {
            /**
             * provider
             * 处理android 7.0 及以上系统安装异常问题
             */
            // File file = new File(apkPath);
            Intent install = new Intent();
            install.setAction(Intent.ACTION_VIEW);
            install.addCategory(Intent.CATEGORY_DEFAULT);
            install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Uri contentUri = HYAppUtils.getProviderUril(context,new File(apkPath));
              //  Uri contentUri = FileProvider.getUriForFile(context, "com.imohoo.njgh.integration.fileprovider", new File(apkPath));
                install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
                install.setDataAndType(contentUri, "application/vnd.android.package-archive");
            } else {
                install.setDataAndType(Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");
            }
            context.startActivity(install);
        } catch (Exception e) {
            Toast.makeText(context, "文件解析失败", Toast.LENGTH_SHORT).show();
        }
    }
}
